<template>
  <div class="addStudent">
    <el-tabs type="border-card">
      <el-tab-pane>
        <span slot="label"><i class="el-icon-date"></i> 我的行程</span>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="学生的学号" prop="id">
            <el-input v-model.number="ruleForm.student_id"></el-input>
          </el-form-item>
          <el-form-item label="学生的姓名" prop="name">
            <el-input v-model.number="ruleForm.student_name"></el-input>
          </el-form-item>
          <el-form-item label="学生的密码" prop="pwd">
            <el-input v-model="ruleForm.student_pwd"></el-input>
          </el-form-item>
          <el-form-item label="学生所在的班级id" prop="gradeid">
            <el-input v-model.number="ruleForm.grade_id"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="消息中心">消息中心</el-tab-pane>
      <el-tab-pane label="角色管理">角色管理</el-tab-pane>
      <el-tab-pane label="定时任务补偿">定时任务补偿</el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { addStudent } from "../../../api/student";

export default {
  data() {
    var checkId = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("内容不能为空"));
      } else {
        callback();
      }
    };
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("内容不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          callback();
        }
      }, 1000);
    };
    var checkPwd = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("内容不能为空"));
      } else {
        callback();
      }
    };
    var checkGradeid = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("内容不能为空"));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        student_id: "",
        student_name: "",
        student_pwd: "",
        grade_id: "",
      },
      rules: {
        student_id: [{ validator: checkId, trigger: "blur" }],
        student_name: [{ validator: checkName, trigger: "blur" }],
        student_pwd: [{ validator: checkPwd, trigger: "blur" }],
        grade_id: [{ validator: checkGradeid, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = this.ruleForm.grade_id
            ? {
                student_id: this.ruleForm.student_id + "",
                student_name: this.ruleForm.student_name,
                student_pwd: this.ruleForm.student_pwd,
                grade_id: this.ruleForm.grade_id,
              }
            : {
                student_id: this.ruleForm.student_id + "",
                student_name: this.ruleForm.student_name,
                student_pwd: this.ruleForm.student_pwd,
              };
          addStudent(params).then((res) => {
            console.log(res);
            if (res.data.msg === '添加学生成功') {
              this.$message({
                message: "添加学生成功",
                type: "success",
              });
              this.ruleForm.student_id = "";
              this.ruleForm.student_name = "";
              this.ruleForm.student_pwd = "";
              this.ruleForm.grade_id = "";
            } else {
              this.$message({
                message: "添加学生失败",
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
</style>